package com.tanjun.data.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.interceptor.KeyGenerator;

import java.lang.reflect.Method;

@Slf4j
public class CustomKeyGenerator implements KeyGenerator {
    /**
     * Generate a key for the given method and its parameters.
     *
     * @param target the target instance
     * @param method the method being called
     * @param params the method parameters (with any var-args expanded)
     * @return a generated key
     */
    @Override
    public Object generate(Object target, Method method, Object... params) {
        log.info("生成缓存key,method:{}",method.getName());
        StringBuilder keyBuilder = new StringBuilder();
        keyBuilder.append(target.getClass().getSimpleName()).append(".");
        keyBuilder.append(method.getName()).append("(");
        for (Object param : params) {
            if (param != null) {
                keyBuilder.append(param.toString()).append(",");
            }
        }
        // 移除最后一个逗号并添加闭括号
        keyBuilder.deleteCharAt(keyBuilder.length() - 1).append(")");
        return keyBuilder.toString();
    }
}
